## Figure 4: Effect of Donations at Time t on Subsequent Lobbying: -------------

## Instructions ----------------------------------------------------------------
## Instructions ----------------------------------------------------------------
# Step 1: Adjust DATA_DIR to where Data is is located
# Step 2: Adjust OUT_DIR to where Output folder is
# Step 3: Run entire script

## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())
# Need to install to read/use PanelEstimate Objects
# devtools::install_github("insongkim/PanelMatch",ref = "big_data_checks")

## library
library(dplyr)
library(data.table)
library(PanelMatch)

# set directory
MAIN_DIR <- "C:/Users/js.egb/Dropbox/campaign-lobby-paper/replication_package"

# set data folder
DATA_DIR <-  paste(MAIN_DIR, "/data/fig4/", sep = "")

# set output folder
OUT_DIR <- paste(MAIN_DIR, "/output/", sep = "")


#####################################################################
## Overall effect: Senate
#####################################################################

load(file.path(DATA_DIR,
               "EstObj_manuf_ann_senate_maha_AnyDon_AnyLob"))

pm_sum <- summary(PM.lobby)$summary # get summary DF from PE object

yaxis_limits <- c(-0.05, 0.15) # Y Axis
xaxis_limits <- c(0.75,4.25) # X Axis
legend_font_size <- 1.25 # Font Size of Legend (if any)
point_size <- 7 # size of points

pdf(file = file.path(OUT_DIR, "figure_4a.pdf"), # pm_overall.pdf
    height = 8, width = 10)
par(font.lab = 1.8, cex.lab = 2.5, cex.axis = 2,
    cex.main = 3, mar = c(6,11,4,2), las=1)

plot(PM.lobby, main = "Senate",
     ylim = yaxis_limits, xlim = xaxis_limits,
     ylab = "", xlab = "", col = "black")

title(ylab = "Estimated Effects of Donation on\nProbability to Lobby", line = 6.5)


mtext(side = 1, "Years relative to the timing of donation",
      line = 3.3, cex=2.2)

## plot again with larger dots and CIs
points(c(1:length(pm_sum[,1])), pm_sum[,1],
       lwd = point_size, col = "black", pch = 19, cex=1.7)
for (j in 1:length(pm_sum[,1])) {
  arrows(x0 = j, x1 = j, y0 =  pm_sum[j,4], y1 = pm_sum[j,3],
         length = 0,lwd = 2, col = "black")
}

dev.off()

rm(pm_sum, PM.lobby)



#####################################################################
## Overall effect: House
#####################################################################


load(file.path(DATA_DIR,
               "EstObj_manuf_ann_house_maha_AnyDon_AnyLob"))

pm_sum <- summary(PM.lobby)$summary # get summary DF from PE object

yaxis_limits <- c(-0.05, 0.15) # Y Axis
xaxis_limits <- c(0.75,4.25) # X Axis
legend_font_size <- 1.25 # Font Size of Legend (if any)
point_size <- 7 # size of points

pdf(file = file.path(OUT_DIR, "figure_4b.pdf"), #pm_overall_house.pdf
    height = 8, width = 10)
par(font.lab = 1.8, cex.lab = 2.5, cex.axis = 2,
    cex.main = 3, mar = c(6,11,4,2), las=1)

plot(PM.lobby, main = "House",
     ylim = yaxis_limits, xlim = xaxis_limits,
     ylab = "", xlab = "", col = "black")

title(ylab = "Estimated Effects of Donation on\nProbability to Lobby", line = 6.5)

## mtext(side = 2, "Estimated Effects of Donation on Lobbying",
##       line = 4, cex=2)

mtext(side = 1, "Years relative to the timing of donation",
      line = 3.3, cex=2.2)

## plot again with larger dots and CIs
points(c(1:length(pm_sum[,1])), pm_sum[,1],
       lwd = point_size, col = "black", pch = 19, cex=1.7)
for (j in 1:length(pm_sum[,1])) {
  arrows(x0 = j, x1 = j, y0 =  pm_sum[j,4], y1 = pm_sum[j,3],
         length = 0,lwd = 2, col = "black")
}

dev.off()



rm(pm_sum, PM.lobby)



